<template>
  <div class="profile-box">
    <!-- 未登录 -->
    <div
      v-if="!user"
      class="not-login header-bg"
    >
      <img
        src="@/assets/mobile.png"
        @click="$router.push('/login')"
      >
      <span>登录 / 注册</span>
    </div>
    <!-- 已登录 -->
    <div
      class="login header-bg"
      v-else
    >
      <div class="user-info">
        <div class="user-container">
          <!-- 左侧盒子 -->
          <div class="left">
            <!-- 头像 -->
            <van-image
              class="avatar"
              round
              width="1rem"
              height="1rem"
              :src="userInfo.photo"
            />
            <!-- 文字 -->
            <span class="username">{{userInfo.name}}</span>
          </div>
          <!-- 右侧按钮 -->
          <van-button
            size="mini"
            round
            type="default"
          >修改资料</van-button>
        </div>
      </div>
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid
      :column-num="2"
      class="nav-grid"
    >
      <van-grid-item>
        <i
          slot="icon"
          class="toutiao toutiao-shoucang"
        ></i>
        <span
          class="grid-text"
          slot="text"
        >收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i
          slot="icon"
          class="toutiao toutiao-lishi"
        ></i>
        <span
          class="grid-text"
          slot="text"
        >历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 消息通知 -->
    <div class="box-cell">
      <van-cell
        title="消息通知"
        is-link
      />
      <van-cell
        title="小爱同学"
        is-link
      />
    </div>
    <!-- 退出登录 -->
    <van-cell
      class="logout-cell"
      title="退出登录"
      v-if="user"
      @click="logoutFn"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserProfile } from '@/api/user.js'
export default {
  name: 'profile',
  data () {
    return {
      // 当前用户的信息
      userInfo: {}
    }
  },
  methods: {
    // 退出登录处理函数
    logoutFn () {
      // 弹窗提醒用户
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确定退出登录?'
      })
        .then(() => {
          // on confirm
          // 移除vuex中的token(会自动将本地的token设置为null)
          this.$store.commit('initUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUser () {
      try {
        const { data: res } = await getUserProfile()
        // 保存到本地
        this.userInfo = res.data
      } catch (error) {
        this.$toast('获取失败')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    if (this.user) {
      // 如果登录了 就加载用户信息
      this.loadUser()
    }
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.profile-box {
  .header-bg {
    width: 750px;
    height: 400px;
    // 背景图片
    background-image: url(~@/assets/banner.png);
    // 设置背景图片大小
    background-size: 100%;
  }
  .not-login {
    // 开启flex布局
    display: flex;
    // 设置主轴居中对齐
    justify-content: center;
    // 设置侧轴居中对齐
    align-items: center;
    // 设置主轴方向为y轴
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 32px;
      color: #fff;
      margin-top: 15px;
    }
  }

  .login {
    .user-info {
      height: 247px;
      padding: 0 32px;
      // 让左侧盒子和右侧按钮距离底部对齐
      display: flex;
      align-items: flex-end;
      .user-container {
        height: 132px;
        display: flex;
        // 左侧盒子和右侧盒子左右两端对齐
        justify-content: space-between;
        width: 100%;
        // 侧轴居中对齐
        align-items: center;
        .left {
          // 图片和文字居中
          display: flex;
          align-items: center;
        }
        .username {
          font-size: 30px;
          color: #fff;
          margin-left: 22px;
        }
      }
    }
    .user-data {
      height: 153px;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        span:nth-child(1) {
          font-size: 30px;
          margin-bottom: 20px;
        }
        span:nth-child(2) {
          font-size: 22px;
        }
      }
    }
  }

  .nav-grid {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .grid-text {
      font-size: 27px;
      color: #333;
      margin-top: 15px;
      letter-spacing: 10px;
    }
  }
  .box-cell {
    margin: 10px 0;
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
  }
}
</style>
